﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Com.Charence.PassManager.Common
{
    public class Passcode
    {
        private Passcode(string code)
        {
            var bytes = Encoding.UTF8.GetBytes(code);

            var sha = new SHA1CryptoServiceProvider();
            var result = sha.ComputeHash(bytes);

            var sb = new StringBuilder(40);
            for (var i = 0; i < result.Length; i++)
            {
                sb.Append(result[i].ToString("x").PadLeft(2, '0'));
            }

            Value = sb.ToString();
        }

        public string Value { get; set; }

        public static implicit operator Passcode(string code)
        {
            return new Passcode(code);
        }

        public override string ToString()
        {
            return this.Value;
        }
    }
}
